package com.pub.tools;

/**
 * UtilityBean.java 08/14/2003
 * @history
 * -----------------------------------------------------------
 * Claire Kuo          08/14/2003          Initial.
 * Tan Chen            08/28/2003          Develop.
 */

import java.io.*;
import java.util.*;


import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.rtf.*;


/**
  * <p>Utility Functions - The Components of Every Common Methods. <br>
  * The UtilityBean allow data transfer to four data type. The data type
  *    include PDF、RTF、CSV、TXT file type.
  * </p>
  * <p> For example,the following are ArrayList data transfer to PDF file formate data  </p>
  * <p>
  * &nbsp;&nbsp;  import java.io.*; <br>
  * &nbsp;&nbsp;  import java.lang.*; <br>
  * &nbsp;&nbsp;  import java.util.*; <br>
  * &nbsp;&nbsp;  import com.alpha.bean.UtilBean; <br> <br>
  *
  * &nbsp;&nbsp; public class testBean { <br>
  * &nbsp;&nbsp;&nbsp;  public static void main(String[] args) throws IOException { <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;  DataOutputStream out = new DataOutputStream(  <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;  new FileOutputStream("C:\\test.pdf")); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     ArrayList list1=new ArrayList(); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     ArrayList list2=new ArrayList(); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     list2.add("col 1"); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     list2.add("col 2"); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     list2.add("col 3"); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     list1.add(list2); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     UserBean ub = new UserBean(); <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     // transfer data use UserBean <br>
  * <b>&nbsp;&nbsp;&nbsp;&nbsp;     out.write(ub.TransferToTxt(list1)); </b><br>
  * &nbsp;&nbsp;&nbsp;&nbsp;     out.close(); <br>
  * &nbsp;&nbsp;&nbsp;   } <br>
  * &nbsp;&nbsp; } <br>
  * </p>
  * @data 08/14/2003
  * @author Claire Kuo
  */

public class UtilityBean {

  /** Get Property from file on default path
    * @param ptyType. The filename include those properties.
    */
  public UtilityBean() {
  }

  /** Return Excel Content-Type
   * @return String.
    */
  public String getExcelType() {
    return "application/vnd.ms-excel; charset=Big5";
  }

  /** Return Word Content-Type
   * @return String.
    */
  public String getWordType() {
    return "application/vnd.ms-word; charset=Big5";
  }

  /** Return Pdf Content-Type
   * @return String.
   */
  public String getPdfType() {
    return "application/pdf; charset=Big5";
  }

  /** set Html data
    * @param data. The source data of ArrayList DataType.
    */
  @SuppressWarnings("unchecked")
public void setHtmlData(ArrayList data) {
    ArrayList<ArrayList<String>> templist=null;
    Vector<String> tempvector=null;
    htmlbody=" <HTML>";
    htmlbody+=" <BODY> ";
    htmlbody+=" <TABLE> ";
    for (int loopi=0;loopi<data.size();loopi++){
      htmlbody+="<tr>";
      if (data.get(loopi).getClass().getName().equals("java.util.ArrayList")) {
        templist = (ArrayList) data.get(loopi);
        for(int loopj=0;loopj<templist.size();loopj++) {
          htmlbody+="<td>"+templist.get(loopj).toString()+"</td>";
        }
      } // if end
      if (data.get(loopi).getClass().getName().equals("java.util.Vector")){
        tempvector = (Vector) data.get(loopi);
        for(int loopj=0;loopj<tempvector.size();loopj++) {
          htmlbody+="<td>"+tempvector.get(loopj).toString()+"</td>";
        }
      } // if end
      htmlbody+="</tr>";
    }  // for end
    htmlbody+=" </TABLE> ";
    htmlbody+=" </BODY> ";
    htmlbody+=" </HTML> ";
  }

  /** Return Html data
   * @return String.
    */
  public String getHtmlData() {
    return htmlbody;
  }

  /** Transfer Data to Excel Formate and return a byte array
    * @param data. The source data of ArrayList DataType.
    */
  @SuppressWarnings("unchecked")
public byte[] TransferToExcel(ArrayList data) {
    String tempdata = "";
    ArrayList<ArrayList<String>> templist = null;
    Vector<String> tempvector = null;

    for (int loopi = 0; loopi < data.size(); loopi++) {
        if (data.get(loopi).getClass().getName().equals("java.util.ArrayList")) {
          templist = (ArrayList<ArrayList<String>>) data.get(loopi);
          for (int loopj = 0; loopj < templist.size(); loopj++) {
            tempdata+=templist.get(loopj).toString()+",";
          }
        } else if (data.get(loopi).getClass().getName().equals("java.util.Vector")) {
          tempvector = (Vector<String>) data.get(loopi);
          for (int loopj = 0; loopj < tempvector.size(); loopj++) {
            tempdata+=tempvector.get(loopj).toString()+",";
          }
        } else {
          tempdata+=data.get(loopi).toString()+",";
        }
        tempdata =tempdata+'\r'+'\n';
      }
      return tempdata.getBytes();
  }

    /** Transfer Data to Txt Formate and return a byte array
    * @param data. The source data of ArrayList DataType.
    */
  @SuppressWarnings("unchecked")
public byte[] TransferToTxt(ArrayList data) {
    String tempdata = "";
    ArrayList<Object> templist = null;
    Vector<Object> tempvector = null;

    for (int loopi = 0; loopi < data.size(); loopi++) {
        if (data.get(loopi).getClass().getName().equals("java.util.ArrayList")) {
          templist = (ArrayList<Object>) data.get(loopi);
          for (int loopj = 0; loopj < templist.size(); loopj++) {
            tempdata+=templist.get(loopj).toString();
          }
        } else if (data.get(loopi).getClass().getName().equals("java.util.Vector")) {
          tempvector = (Vector<Object>) data.get(loopi);
          for (int loopj = 0; loopj < tempvector.size(); loopj++) {
            tempdata+=tempvector.get(loopj).toString();
          }
        } else {
          tempdata+=data.get(loopi).toString();
        }
        tempdata =tempdata+'\r'+'\n';
      }
      return tempdata.getBytes();
  }

  /** Transfer Data to Pdf Formate and return a byte array
    * @param data. The source data of ArrayList DataType.
    */
  @SuppressWarnings("unchecked")
public byte[] TransferToPdf(ArrayList data) {
    ArrayList<ArrayList<String>> templist = null;
    Vector<String> tempvector = null;
    int colsize = 0;
    if (data.get(0).getClass().getName().equals("java.util.ArrayList")) {
      templist = (ArrayList<ArrayList<String>>)data.get(0);
      colsize = templist.size();
    } else if (data.get(0).getClass().getName().equals("java.util.Vector")) {
      tempvector = (Vector<String>)data.get(0);
      colsize = tempvector.size();
    } else {
      colsize = data.size();
    }
    ByteArrayOutputStream barray = new ByteArrayOutputStream();
    Document document=new Document(PageSize.A4,50,50,50,50);
    try {
      PdfWriter.getInstance(document,barray);
      document.open();
      Table datatable = new Table(colsize);
      datatable.setPadding(2);
      datatable.setSpacing(0);
      for (int loopi = 0; loopi < data.size(); loopi++) {
        if (data.get(loopi).getClass().getName().equals("java.util.ArrayList")) {
          templist = (ArrayList<ArrayList<String>>) data.get(loopi);
          for (int loopj = 0; loopj < templist.size(); loopj++) {
            datatable.addCell(templist.get(loopj).toString());
          }
        } else if (data.get(loopi).getClass().getName().equals("java.util.Vector")) {
          tempvector = (Vector<String>) data.get(loopi);
          for (int loopj = 0; loopj < tempvector.size(); loopj++) {
            datatable.addCell(tempvector.get(loopj).toString());
          }
        } else {
          datatable.addCell(data.get(loopi).toString());
        }
      }
      document.add(datatable);
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
    return barray.toByteArray();
  }

  /** Transfer Data to Rtf Formate and return a byte array
    * @param data. The source data of ArrayList DataType.
    */
  @SuppressWarnings("unchecked")
public byte[] TransferToRtf(ArrayList data) {
    ArrayList<ArrayList<String>> templist = null;
    Vector<String> tempvector = null;
    int colsize = 0;
    if (data.get(0).getClass().getName().equals("java.util.ArrayList")) {
      templist = (ArrayList<ArrayList<String>>)data.get(0);
      colsize = templist.size();
    } else  if (data.get(0).getClass().getName().equals("java.util.Vector")) {
      tempvector = (Vector<String>)data.get(0);
      colsize = tempvector.size();
    } else {
      colsize = data.size();
    }

    ByteArrayOutputStream barray = new ByteArrayOutputStream();
    Document document=new Document(PageSize.A4,50,50,50,50);
    try {
      RtfWriter.getInstance(document,barray);
      document.open();
      Table datatable = new Table(colsize);
      datatable.setPadding(2);
      datatable.setSpacing(0);
      for (int loopi = 0; loopi < data.size(); loopi++) {
        if (data.get(loopi).getClass().getName().equals("java.util.ArrayList")) {
          templist = (ArrayList<ArrayList<String>>) data.get(loopi);
          for (int loopj = 0; loopj < templist.size(); loopj++) {
            datatable.addCell(templist.get(loopj).toString());
          }
        } else if (data.get(loopi).getClass().getName().equals("java.util.Vector")) {
          tempvector = (Vector<String>) data.get(loopi);
          for (int loopj = 0; loopj < tempvector.size(); loopj++) {
            datatable.addCell(tempvector.get(loopj).toString());
          }
        } else {
          datatable.addCell(data.get(loopi).toString());
        }
      }
      document.add(datatable);
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
    return barray.toByteArray();
  }

  private String htmlbody="";

}
